package com.hu.chat.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hu.chat.model.R;
import com.hu.chat.model.base.User;
import com.hu.chat.model.po.UserLoginPo;
import com.hu.chat.service.UserService;
import com.hu.chat.service.WebSocketService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 功能描述：
 *
 * @Author huyouzhong
 * @Date 2024/1/3 11:15
 */
@RestController
public class LoginController {

    @Resource
    UserService userService;

    @PostMapping("/login")
    public R<Object> login(@RequestBody UserLoginPo po) {
        User one = userService.getOne(new LambdaQueryWrapper<User>()
                .eq(User::getId, po.getUsername())
                .eq(User::getPassword, po.getPassword()));

        if (one == null || WebSocketService.getUserSessionMap().containsKey(one.getId())) {
            return R.fail().message("错误");
        }

        return R.ok();
    }

}
